<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

//订单控制器
class Order extends Home_Controller{

	public function __construct(){
		parent::__construct();
        $this->load->model('goods_model');
        $this->load->model('orders_model');
	}
    public function index(){
        //$data['goods'] = $this->input->post("goods");
        //看session是否存在当前用户user_id，不存在就让其登录
        //当前用户user_id
        $user=$this->session->userdata('user');
        if(empty($user)){
            //请登录
            exit("请登录");
        }

        $goods = $this->input->post("goods");
        //var_dump($goods);
        $data = array();
        $goods_name_arr = array();
        $total_fee_arr = array();
        //生成订单编号
        $data['out_trade_no'] =$trade_no= 'BPH-' . date('YmdHis').'-' . uniqid();
        //商品id
        $goods_id_arr = array();
        //商品单价
        $shop_price_arr = array();
        //商品数量
        $qty_arr = array();
        foreach ($goods as $k=>$v){
            $explode_name = explode("#@#",$v);
            $goods_id = $explode_name[1];
            $get_good = $this->goods_model->get_goods($goods_id);
            if(empty($get_good)){
                exit("非法操作");
            }
            $data['goods'][$k]['goods_id'] = $goods_id;
            $data['goods'][$k]['goods_name'] = $explode_name[0];
            $data['goods'][$k]['shop_price'] = $get_good['shop_price'];
            $data['goods'][$k]['qty'] = $explode_name[2];
            $data['goods'][$k]['total_fee'] = $explode_name[2]*$get_good['shop_price'];
            array_push($goods_name_arr,$explode_name[0]);
            array_push($total_fee_arr,$explode_name[2]*$get_good['shop_price']);

            array_push($goods_id_arr,$goods_id);
            array_push($shop_price_arr,$get_good['shop_price']);
            array_push($qty_arr,$explode_name[2]);

        }

        $data['subject'] =$subjects= implode("#@#",$goods_name_arr);
        $data['total_price'] =$total= array_sum($total_fee_arr);

        if(!empty($goods_id_arr)){

            //写入订单表中
            //生成订单

            //收货人
            $receive = $this->input->post('receive');
            //收货地址
            $address = $this->input->post('address');
            //电话
            $phone = $this->input->post('phone');

            $order=array(
                'trade_no'=>$trade_no,				//订单号
                'total'=>$total,					//订单总金额
                'receive'=>$receive,		//收货人
                'address'=>$address,		//收货地址
                'phone'=>$phone,		//电话
                'ctime'=>time(),					//下单时间
                'pay_status'=>0,						//支付状态 0 未支付  1已付款
                'goods_id'=>implode("#@#",$goods_id_arr),			//商品ID
                'goods_names'=>$subjects,                            //产品名称
                'price'=>implode("#@#",$shop_price_arr),		//单价
                'qty'=>implode("#@#",$qty_arr),					//数量
                'user_id'=>$user['user_id']
            );
            if($this->orders_model->add_order($order)){
                //说明下单成功
            }else{
                die("服务器错误，下单失败! <a href='javascript:history.go(-1)'>返回</a>");
            }
        }else{
            exit("非法操作");
        }


        $this->load->view('order.html',$data);
    }

}